package com.http;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.os.AsyncTask;

import com.android.ocean.util.common.StringUtil;
import com.android.ocean.util.net.ErrorCode;
import com.android.ocean.util.net.NetConUtil;
import com.android.ocean.util.net.ResEntity;
import com.android.ocean.util.net.ResObj;
import com.common.method.CommonData;
import com.common.method.ServerPath;
import com.modle.YzmStruct;
import com.util.JsonUtil;

/**
 * 
 * @ClassName:YzmGetTask
 * @Description:TODO获取验证码
 * @author zhuw
 * @date 2013-9-9 下午10:05:25
 */
public class YzmGetTask extends AsyncTask<String, Void, ResultYzmStruct> {
	public static interface CallBack extends NetWorkCallBack {
		public abstract void procYzm(YzmStruct data);

	}

	CallBack callback;

	public YzmGetTask(CallBack callback) {
		super();
		this.callback = callback;
	}

	@Override
	protected ResultYzmStruct doInBackground(String... params) {
		ResultYzmStruct data = null;
		try {
			ArrayList<NameValuePair> paramPairs = new ArrayList<NameValuePair>();
			paramPairs.add(new BasicNameValuePair("mobile", params[0]));
			ResEntity re = NetConUtil.postByForm(ServerPath.YZM_PATH,
					paramPairs);
			if (re.code == ResEntity.S_OK) {

				data = new ResultYzmStruct(CommonData.H_FLAG_SUCCESS, null,
						JsonUtil.parseYzmStruct(new JSONObject(re.jsonStr)));
			} else if (re.code == ResEntity.S_FAILED) {

				data = new ResultYzmStruct(CommonData.H_FLAG_ERROR, "code:"
						+ re.code + "    " + StringUtil.unicodeToString(re.jsonStr), null);

			} else {
				data = new ResultYzmStruct(CommonData.H_FLAG_ERROR, "code:"
						+ re.code + ErrorCode.ERROR_MSG, null);
			}
		} catch (Exception e) {
			data = new ResultYzmStruct(CommonData.H_FLAG_ERROR,
					ErrorCode.ERROR_MSG, null);
			e.printStackTrace();

		}
		return data;
	}

	@Override
	protected void onPostExecute(ResultYzmStruct result) {
		if (result.flag == CommonData.H_FLAG_SUCCESS) {
			callback.procYzm(result.data);

		} else {
			callback.procError(result.msg);
		}

	}
}

class ResultYzmStruct extends ResObj {
	public YzmStruct data;

	public ResultYzmStruct(int flag, String msg, YzmStruct data) {
		super();
		this.flag = flag;
		this.msg = msg;
		this.data = data;
	}

}
